package com.mystore.service.impl;

import com.mystore.dao.OrderDao;
import com.mystore.dao.impl.OrderDaoImpl;
import com.mystore.entity.Order;
import com.mystore.service.GoodService;
import com.mystore.service.OrderService;
import com.mystore.util.DBUtil;
import com.mystore.vo.OrderVO;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class OrderServiceImpl implements OrderService {

    @Override
    public List<OrderVO> findMyOrder(int uid) {
        Connection connection = DBUtil.getConntion();
        OrderDao orderDao = new OrderDaoImpl();
        List<OrderVO> list = orderDao.findAllOrderByUid(uid,connection);
        DBUtil.closeConnection(connection);
        return list;
    }

    @Override
    public Boolean addOrder(Order order) throws SQLException {
        Connection connection = DBUtil.getConntion();
        OrderDao orderDao = new OrderDaoImpl();
        GoodService goodService = new GoodServiceImpl();
        //获取商品的价格信息
        double price = goodService.findByid(order.getGid()).getPrice();
        order.setPrice(price);
        Boolean issave = orderDao.save(order,connection);
        DBUtil.closeConnection(connection);
        return issave;
    }

    @Override
    public OrderVO findOneOrderByOidAndUid(int oid, int uid) {
        Connection connection = DBUtil.getConntion();
        OrderDao orderDao = new OrderDaoImpl();
        OrderVO orderVO = orderDao.findOneOrderByOidAndUid(oid,uid,connection);
        DBUtil.closeConnection(connection);
        return orderVO;
    }

    @Override
    public Boolean updateOrder(Order order) throws SQLException {
        Connection connection = DBUtil.getConntion();
        OrderDao orderDao = new OrderDaoImpl();
        Boolean isupdate = orderDao.updateOrder(order,connection);
        DBUtil.closeConnection(connection);
        return isupdate;
    }
}
